//
//  FolderViewController.m
//  FinderForiOS
//
//  Created by zhen7216 on 2017/7/3.
//  Copyright © 2017年 chenzhen. All rights reserved.
//

#import "FolderViewController.h"

/*
 1.遍历目录
 2.创建目录
 3.删除目录
 4.写入文件
 5.删除文件
 6.读取文件
 */

static NSString *folderCellIdentifier = @"folder_cell_identifier";

@interface FolderViewController ()
@property (nonatomic, copy) NSString *dir;
@property (nonatomic, copy) NSArray *items;
@end

@implementation FolderViewController

- (instancetype)initWithDir:(NSString *)dir {
    if (self = [super init]) {
        _dir = [dir copy];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setup];
    [self reflesh];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UI Setup
- (void)setup {
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:folderCellIdentifier];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"添加" style:UIBarButtonItemStylePlain target:self action:@selector(add:)];
}


#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *name = self.items[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:folderCellIdentifier];
    cell.textLabel.text = name;
    return cell;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        NSString *name = _items[indexPath.row];
        [self remove:name];
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *name = _items[indexPath.row];
    NSString *path = [self.dir stringByAppendingPathComponent:name];
    BOOL isDir = NO;
    if ([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir]) {
        if (isDir) {
            FolderViewController *vc = [[FolderViewController alloc] initWithDir:path];
            [self.navigationController pushViewController:vc animated:YES];
        } else {
            NSData *data = [NSData dataWithContentsOfFile:path];
            NSString *content = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"%@", content);
        }
    }
}

#pragma mark - misc
- (void)reflesh {
    self.items = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.dir error:nil];
    [self.tableView reloadData];
}

- (void)add:(id)sender {
    NSString *content = @"helloWorld";
    NSString *path = [self pathByName:@"helloworld"];
    NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
    [data writeToFile:path atomically:YES];
    [self reflesh];
}

- (void)remove:(NSString *)name {
    NSError *error = nil;
    NSString *path = [self.dir stringByAppendingPathComponent:name];
    [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
    NSLog(@"%@",error);
    [self reflesh];
}

- (NSString *)pathByName:(NSString *)name {
    NSString *basePath = [self.dir stringByAppendingPathComponent:name];
    NSString *path = basePath;
    NSInteger index = 1;
    while ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
        path = [basePath stringByAppendingFormat:@"-%zd", index];
        index++;
    }
    return path;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
